package qimao.com.edu.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import qimao.com.edu.dao.AdminDao;
import qimao.com.edu.dao.BaseDao;
import qimao.com.edu.entity.Admin;
import qimao.com.edu.entity.Author;

public class AdminDaoImpl implements AdminDao {

	@Override
	public Admin selectAdminByName(Admin admin) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Admin adm = null;
		int result = 0;
		String sql = "SELECT * FROM admin WHERE username = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
	        ps.setString(1, admin.getUsername());
			rs = ps.executeQuery();
			if(rs.next()) {
				adm =new Admin();
				adm.setAdminId(rs.getInt("admin_id"));
				adm.setUsername(rs.getString("username"));
				adm.setPassword(rs.getString("password"));
				adm.setRoleName(rs.getString("rolename"));
				adm.setNickname(rs.getString("nickname"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, rs);
		}
		return adm;
	}

	@Override
	public Admin selectAdmin(Admin admin) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Admin adm = null;
		String sql = "SELECT * FROM admin WHERE username = ? and password = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, admin.getUsername());
			ps.setString(2, admin.getPassword());
			System.out.println(ps);
			rs = ps.executeQuery();
			if(rs.next()) {
				adm =new Admin();
				adm.setAdminId(rs.getInt("admin_id"));
				adm.setUsername(rs.getString("username"));
				adm.setPassword(rs.getString("password"));
				adm.setRoleName(rs.getString("rolename"));
				adm.setNickname(rs.getString("nickname"));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, rs);
		}
		return adm;
	}

	
}
